// pages/home/index.js
const app = getApp()
var mid;
var sid;
var rid;
var uid;
var page = 1;
var pagesize = 12;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canse: false,
    canUse: wx.canIUse('button.open-type.getUserInfo'),
    userBasic: {},
    userList:[],
    bagImg:[
      '/images/icon/giveup.png',
      '/images/icon/give.png',
      '/images/icon/browse.png'

    ],
  },
  baseImage:function(){
    var that = this;
    var data = that.data.bagImg;
    console.log('转码', data)
    for (var i = 0; i < data.length;i++){
      var bgm = app.baseImage(data[i]);
      data[i] = bgm;
    }
    that.setData({
      bagImg: data
    })
  },
  getStorage:function(){
    var that = this;
    try {
      const value = wx.getStorageSync('userInfo')
      if (value) {
        uid = value.uid;
        that.getUserList();
        that.getUserBasicInfo();
      }else{
        that.getUserInfo();
      }
    } catch (e) {
      app.prompt('登陆失败',e)
      // Do something when catch error
    }
  },

  getSarchInterface: function () {
    wx.navigateTo({
      url: '../sarch/index'
    })
  },
  getSarchCode: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      scanType: [],
      success: function (res) {
        console.log(res)
        var data = []
        var url = ''
        data['data'] = res
        var calkdata = app.netWorkRequest(that,data, url)
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log(res)
      },
    })
  },
  goMessage:function(){
    wx.navigateTo({
      url: '../message/index'
    })
  },
  goVisitorInfo: function (e) {
    console.log('传递信息', e.currentTarget.id)
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../visitorInfo/index?id=' + id
    })
  },
  //获取用户列表
  getUserList:function(){
    var that = this;
    var data = [];
    data['uid'] = uid;
    data['page'] = page;
    data['pagesize'] = pagesize;
    var url = 'We_Community/getRecommendUserInfo';
    var calkdata = app.netWorkRequest(that,data, url, 'userList')
  },
  //加载更多用户列表
  getUserListLoadMore: function () {
    console.log('椰树', page)
    var that = this;
    var data = [];
    // 页数+1
    page = page + 1;
    data['uid'] = uid;
    data['page'] = page;
    data['pagesize'] = pagesize;
    var url = 'We_Community/getRecommendUserInfo';
    var f_data = that.data.userList;
    var calkdata = app.netWorkRequestLoadMore(that, data, url, 'userList', f_data);
  },

  /**
 * 用户授权
 */
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      this.getUserInfo();
    } else {
      app.prompt('登陆失败');
    }
  },

  /**
    * 授权获取用户信息
    */
  getUserInfo: function () {
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log('已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框')
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          app.getUserInfo(that);
        } else {
          that.setData({
            canse: true
          })
        }
      }
    })
  },
  //获取个人基本信息
  getUserBasicInfo: function () {
    var that = this;
    var data = [];
    var url = 'We_Community/getBrowsePersonalUserBasic'
    var calkdata = app.netWorkRequest(that, data, url, 'userBasic')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('start加载获取转发消息', options, '分享者ID:' + options.sid + '访客ID:' + options.mid + '红包ID:' + options.rid)
    mid = options.mid;
    sid = options.sid;
    rid = options.rid;
    if (mid && mid != undefined && mid != 'undefined')
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.baseImage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('监听页面显示')
    page = 1;
    console.log('页面加载')
    this.setData({
      canse:false
    })
    this.getStorage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    this.getUserListLoadMore();
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options ) {
    console.log('分享', options)
  }
  
})